using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;


namespace CommerceForUmbraco.usercontrols
{
    public partial class PaymentBox : System.Web.UI.UserControl
    {


        //private string ccNumber;

        public string CCNumber
        {
            get
            {
                return txtCCNumber.Text;
            }
        }
        //private int expMonth;

        public int ExpirationMonth
        {
            get
            {
                int iOut = 0;
                int.TryParse(ddlExpMonth.SelectedValue, out iOut);
                return iOut;
            }
        }

        public CreditCardType CCType
        {
            get
            {
                // determine CCType from user input
                return Utility.GetCardType(CleanData(txtCCNumber.Text));
            }
        }

        public int ExpirationYear
        {
            get
            {
                int iOut = 0;
                int.TryParse(ddlExpYear.SelectedValue, out iOut);
                return iOut;
            }
        }

        public string SecurityCode
        {
            get
            {
                return txtCCAuthCode.Text;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadExpirationYear();
            }
        }
        void LoadExpirationYear()
        {

            if (ddlExpYear != null)
            {
                for (int i = DateTime.UtcNow.Year; i < DateTime.UtcNow.Year + 15; i++)
                {
                    ddlExpYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
            }
        }

        void SetSelectedMonth()
        {
            int selectedMonth = (DateTime.Now.Month + 1);
            ddlExpMonth.SelectedValue = selectedMonth.ToString();
        }

        /// <summary>
        /// Cleans the data of spaces and non-numerics
        /// </summary>
        /// <param name="data">string data.</param>
        /// <returns></returns>
        protected string CleanData(string data)
        {
            string output = "";
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"[\s+]?[\.+]?[\-+]?");
            output = reg.Replace(data, "");

            return output;
        }

    }
}